package com.wx.mall.operate.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.DisUseDto;
import com.wx.mall.operate.dto.DisUseTwoDto;
import com.wx.mall.operate.entity.DisUse;
import com.wx.mall.operate.service.IDisUseService;
import com.wx.mall.operate.vo.DisUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 优惠券用户关联 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-13
 */
@RestController
@RequestMapping("/dis-use")
@Api(tags = "优惠券用户关联表",description = "DisUseController")
@Slf4j
public class DisUseController {

    @Autowired
    private IDisUseService service;


    @ApiOperation("添加数据")
    @PostMapping("/addList")
    public CommonResult addOne(DisUseDto dto){
        System.out.println(dto);
        service.addOne(dto);
        return  CommonResult.success();
    };


    @ApiOperation("获取数据列表（带筛选和排序）")
    @GetMapping("/getList")
    public CommonResult<CommonPage<DisUse>> getList(@ApiParam("获取dto数据")DisUseTwoDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

        List<DisUse> list=service.getList(dto);

        return CommonResult.success(CommonPage.restPage(list));


    };

    @ApiOperation("根据userid获取数据")
    @GetMapping("/getByUserId")
    public CommonResult getByUserId(Long id){

        DisUserVo vo=service.getByUserId(id);

        return CommonResult.success(vo);

    }


}

